package com.example.casper.Experiment2024.Test;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;

public class ImageUtils {
    private static Bitmap resizeBitmap(Bitmap bitmap) {
        return Bitmap.createScaledBitmap(bitmap, 32, 32, true);
    }
    private static Bitmap toGrayscale(Bitmap bitmap) {
        Bitmap grayBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        for (int x = 0; x < bitmap.getWidth(); x++) {
            for (int y = 0; y < bitmap.getHeight(); y++) {
                int pixel = bitmap.getPixel(x, y);
                int gray = (int) (Color.red(pixel) * 0.299 + Color.green(pixel) * 0.587 + Color.blue(pixel) * 0.114);
                int newPixel = Color.rgb(gray, gray, gray);
                grayBitmap.setPixel(x, y, newPixel);
            }
        }
        return grayBitmap;
    }

    // 计算感知哈希 (pHash)
    public static String getPerceptualHash(Bitmap bitmap) {
        try {
            Bitmap resizedBitmap = resizeBitmap(bitmap);
            Bitmap grayBitmap = toGrayscale(resizedBitmap);
            int width = grayBitmap.getWidth();
            int height = grayBitmap.getHeight();
            int total = 0;
            int[] pixels = new int[width * height];
            grayBitmap.getPixels(pixels, 0, width, 0, 0, width, height);
            for (int pixel : pixels) {
                total += Color.red(pixel); // 灰度图 R=G=B，只取 R
            }
            int avg = total / pixels.length;
            StringBuilder hash = new StringBuilder();
            for (int pixel : pixels) {
                hash.append(Color.red(pixel) >= avg ? "1" : "0");
            }
            return hash.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static boolean areImagesSame(Bitmap img1, Bitmap img2) {
        String hash1 = getPerceptualHash(img1);
        String hash2 = getPerceptualHash(img2);
        if (hash1 == null || hash2 == null) return false;
        int hammingDistance = 0;
        for (int i = 0; i < hash1.length(); i++) {
            if (hash1.charAt(i) != hash2.charAt(i)) {
                hammingDistance++;
            }
        }
        return hammingDistance <= 10; // 阈值可调整
    }
    public static Bitmap getCoverBitmap(int coverResourceId, Context context) {
        Resources res = context.getResources();
        return BitmapFactory.decodeResource(res, coverResourceId);
    }
}

/*
        onView(withId(R.id.recycler_view_the_books))
                .perform(actionOnItemAtPosition(0, // 替换为目标位置
                        new ViewAction() {
                            @Override
                            public Matcher<View> getConstraints() {
                                return isAssignableFrom(ImageView.class);
                            }

                            @Override
                            public String getDescription() {
                                return "Check image in item at position 0";
                            }

                            @Override
                            public void perform(UiController uiController, View view) {
                                ImageView imageView = view.findViewById(R.id.image_view_book_cover);
                                Bitmap actualBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
                                assertTrue(areImagesSame(getCoverBitmap(R.drawable.book_no_name), actualBitmap));
                            }
                        }
                ));
* */